/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.beans;

import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Mateusz
 */
@ManagedBean
@SessionScoped
public class UserBean {
     protected String firstName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
     protected String lastName;
     protected Date dob;
     protected String email;
     
    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

   
    

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
     
    public void validateEmail(FacesContext context, UIComponent toValidate,  Object value) throws ValidatorException{
        String emailStr = (String)value;
        if(emailStr.indexOf("@")==-1){
            FacesMessage message = new FacesMessage("Niewłaściwy adres");
            throw new ValidatorException(message);
        }
            
    }
    public String addConfirmedUSer(){
        //boolean added = true;
        FacesMessage doneMessage = null;
        String outcome = null;
        Users u = new Users();
        UserDAO udao = new UserDAO();
        u.setFirstname(firstName);
        u.setLastname(lastName);
        u.setEmail(email);
        u.setDateofbirth(dob);
        if(udao.insertData(u)){
            doneMessage = new FacesMessage("Doodano uzytkownika");
            outcome = "done";
        }else{
            doneMessage = new FacesMessage("Niepowodzenie");
            outcome = "register";
        }
        FacesContext.getCurrentInstance().addMessage(null, doneMessage);
        return outcome;
    }
    /**
     * Creates a new instance of UserBean
     */
    public UserBean() {
    }
}
